﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProcessM
{
    public partial class Form1 : Form
    {
        Columns myColumns=new Columns();        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitColumns();
            InitListView();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            this.timer1.Interval = 1000;
            this.timer1.Start();
        }

        private void InitColumns()
        {
            myColumns.Pid = true;
            myColumns.User = true;
            myColumns.CPU = true;
            myColumns.Memory = true;
            myColumns.Comment = true;
        }

        private void InitListView()
        {
            this.lvProcesses.BeginUpdate();
            if (myColumns.Pid) this.lvProcesses.Columns.Add("Pid", 50);
            if (myColumns.User) this.lvProcesses.Columns.Add("用户名", 100);
            if (myColumns.CPU) this.lvProcesses.Columns.Add("CPU", 50);
            if (myColumns.Memory) this.lvProcesses.Columns.Add("内存", 80);
            if (myColumns.Comment) this.lvProcesses.Columns.Add("描述", 300);
            this.lvProcesses.EndUpdate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            GetProcesses();
        }

        private void GetProcesses()
        {
            this.lvProcesses.Items.Clear();
            Process[] processes = Process.GetProcesses();
            foreach (Process process in processes)
            {
                try
                {
                    ListViewItem newItem = new ListViewItem();
                    newItem.SubItems[0].Text = process.MainModule.ModuleName;
                    if (myColumns.Pid) newItem.SubItems.Add(process.Id.ToString());
                    if (myColumns.User) newItem.SubItems.Add(Environment.UserName);
                    if (myColumns.CPU) newItem.SubItems.Add(UsingPerfmon(process.ProcessName));
                    if (myColumns.Memory) newItem.SubItems.Add((process.WorkingSet64 / 1000).ToString());
                    if (myColumns.Comment) newItem.SubItems.Add(process.MainWindowTitle);

                    this.lvProcesses.Items.Add(newItem);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    continue;
                }
            }
            this.toolStripStatusLabel1.Text = processes.Length.ToString();
        }
        protected static string UsingPerfmon(string pname)
        {
            //创建性能计数器
            using (var p1 = new PerformanceCounter("Process", "% Processor Time", pname))
            {
                //注意除以CPU数量
                p1.NextValue();
                return (p1.NextValue()).ToString();
            }
        }
    }

    public struct Columns
    {
        public bool Pid;
        public bool User;
        public bool CPU;
        public bool Memory;
        public bool Comment;
    }
}
